package com.zkt.weichat.handler;

import jakarta.annotation.Resource;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author 赵开泰
 * @program jc-club
 * @date 2025/3/23 01:37
 * @description 微信消息工厂类
 **/

@Component
public class WeichatMsgFactory implements InitializingBean {
	
	@Resource
	private List<WeichatMsgHandler> weichatMsgHandlerList;
	
	private Map<WeichatMsgTypeEnum, WeichatMsgHandler> handlerMap = new HashMap<>();
	
	public WeichatMsgHandler getHandlerByMsgType(String msgType) {
		WeichatMsgTypeEnum msgTypeEnum = WeichatMsgTypeEnum.getByMsgType(msgType);
		return handlerMap.get(msgTypeEnum);
	}
	
	@Override
	public void afterPropertiesSet() throws Exception {
		weichatMsgHandlerList.forEach(weichatMsgHandler -> handlerMap.put(weichatMsgHandler.getMsgType(), weichatMsgHandler));
	}
}